package com.angel.architecture.syncronization;

import com.angel.architecture.flex.serialization.SerializationType;
import com.angel.architecture.flex.serialization.property.ClassPropertyIntrospector;
import com.angel.architecture.flex.serialization.property.PropertyAccessor;
import com.angel.architecture.syncronization.annotations.ChangeDetection;
import com.angel.common.helpers.AnnotationHelper;

import org.hibernate.Hibernate;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Map;

public class AnnotationSelectionStrategy implements PropertySelectionStrategy {
    public boolean appliesChangeDetection(Object entity, String propertyName, String[] eventIdentifier) {
        Class<?> clazz = Hibernate.getClass(entity);
        Map<String, PropertyAccessor> properties = ClassPropertyIntrospector.introspect(clazz, SerializationType.BOTH);
        Collection<Annotation> annotations = properties.get(propertyName).getAnnotations(entity);
        ChangeDetection annotation = AnnotationHelper.getAnnotation(annotations, ChangeDetection.class);

        if (annotation != null) {
            eventIdentifier[0] = annotation.value();
            return true;
        }

        return false;
    }
}
